using Commons.Models.Settings.Dtos.Functions;
using Commons.Models.Functions;

namespace Commons.Models.Settings.Mappers.Functions
{
	public class FunctionMapper : IMapper<FunctionDto, IFunction<double, double>>
	{
		public IFunction<double, double> Map(FunctionDto config)
		{
			if (config.Constant != null)
			{
				return new ConstantFunction(config.Constant.Value);
			}

			if (config.Linear != null)
			{
				return new LinearFunction(config.Linear.M, config.Linear.B, config.Linear.Var);
			}

			if (config.Polynomial != null)
			{
				return new PolynomialFunction(config.Polynomial.Coefficients, config.Polynomial.Var);
			}

			if (config.Step != null)
			{
				return new StepFunction(config.Step.Points, config.Step.Var);
			}

			if (config.Piecewise != null)
			{
				PiecewiseFunction function = new PiecewiseFunction(config.Piecewise.Var);

				foreach (EntryDto entry in config.Piecewise.Entries)
				{
					function.AddPiece(entry.X, Map(entry));
				}

				return function;
			}

			return null;
		}
	}
}